module MC.Order {
	export class comCheck {
		public constructor() {
			MC.apiD('comCheck', {
				out_trade_no: MC.get('out_trade_no'),
				lishiid:MC.get('lishiid')
			}, (data: any) => {
				this.loadData(data);
				this.out_trade_no =MC.get('out_trade_no');
				this.lishiid=MC.get('lishiid');
				this.init();
			});

		}
		private stylelist: MC.Order.Table = new MC.Order.Table($('#stylelistDiv'));
		private sizelist: MC.Order.Table = new MC.Order.Table($('#sizelistDiv'));
		private data: any = null;
		private loadData(data: any) {
			//	console.log(data);

			this.data = data;

			$('.orderDockingPage .orderParamTitle .lishibtn').attr('href','/h5/order/comCheck_lishi.html?out_trade_no='+data.out_trade_no);
		
				$('[name=name]').text(this.data.name);
				$('[name=com_name]').text(this.data.com_name);
				$('[name=period]').text(this.data.period);
			
			if (data.type == 'gs') {
				$('.typename').text("公司");
			} else {
				$('.typename').text("工厂");
			}
			if(this.data.read_only){
				this.stylelist.readOnly=true;
				this.sizelist.readOnly=true;
			}

			let stylelist_Data = null;
			let sizelist_Data = null;
			let thrumlist_Data: any = null;
			$('#check_uid').val(data.check_uid);
			if (data.info) {
				let info = data.info;
				
				$('[name=fuzeren_name]').val(info.fuzeren_name);
				$('[name=fuzeren_tel]').val(info.fuzeren_tel);
				$('[name=detail]').val(info.detail);

                $('[name=wanchengdu]').val(info.wanchengdu);
                $('[name=staffTotalNum]').val(info.staffTotalNum);
                $('[name=wanchengdu]').val(info.wanchengdu);
				$('[name=staffStyleNum]').val(info.staffStyleNum);
				$('[name=finishTime]').val(info.finishTime);

				stylelist_Data = info.stylelist;
				sizelist_Data = info.sizelist;

				thrumlist_Data = info.thrumlist;
			}
			this.stylelist.inputtext = true;
			this.stylelist.hangText = false;
			this.stylelist.lieText = '自定义';
			this.stylelist.init();
			if (stylelist_Data && stylelist_Data !== '') {
				this.stylelist.setData(stylelist_Data);
			} else {
				this.stylelist.sethang('数值');
				this.stylelist.setlie('款号,面料,辅料,订单数量,裁剪数量,生产本款人数,裁制当天下货数量,后道当天送货数量,本款送货总数');
			}
			this.stylelist.create();




			this.sizelist.inputtext = true;
			this.sizelist.hangText = '自定义尺寸';
			this.sizelist.lieText = '自定义标题';
			this.sizelist.init();
			if (sizelist_Data && sizelist_Data !== '') {
				this.sizelist.setData(sizelist_Data);
			} else {
				this.sizelist.sethang('M,L,XL,XXL');
				this.sizelist.setlie('部位,领围/腰围,袖长/臀围,后中长/裤长,胸围/拉链,下摆/脚口');
			}
			this.sizelist.create();



			function setradio(name: string) {
				let val = thrumlist_Data[name];
				if (val) {
					$('[name="' + name + '"][value="' + val + '"]').prop('checked', true);
				}
			}

			function setval(name: string) {
				let val = thrumlist_Data[name];
				if (val) {
					$('[name="' + name + '"]').val(val);
				}
			}

			if (thrumlist_Data && thrumlist_Data !== '') {
				thrumlist_Data = JSON.parse(thrumlist_Data);
				setradio('devote');
				setradio('hair');
				setradio('ironing');
				setradio('list');

				setradio('pack');
				setradio('size');
				setradio('smear');
				setradio('tangent');
				setradio('thrum');


				setval('devote_text1');
				setval('devote_text2');
				setval('hair_text1');

				setval('hair_text2');
				setval('ironing_text1');
				setval('ironing_text2');

				setval('list_text1');
				setval('list_text2');
				setval('pack_text');

				setval('pack_text2');
				setval('size_text1');
				setval('size_text2');
				setval('smear_text1');
				setval('smear_text2');

				setval('tangent_text1');
				setval('tangent_text2');
				setval('thrum_text1');
				setval('size_text2');
				setval('thrum_text2');
			}


		}








		private init() {
			if (($('#check_uid').val() == ''||$('#check_uid').val() == '0') && $('#type').val() === 'gs') {
				if(!this.data.read_only){
					this.showxw();	
				}else{
					MC.msg.alert("需要发单方确认填写权限后,才能修改",()=>{
						MC.back();
					});
				}				
			}
			
			
			if (this.data.read_only) {///表示只读模式
				$('.comCheckDiv [type=text],.comCheckDiv textarea').attr('readonly', 'readonly');
				$('.comCheckDiv [type=number]').attr('readonly', 'readonly');
				$('.comCheckDiv input,.comCheckDiv textarea').attr('placeholder', '');
				$('.comCheckDiv [type=radio]').attr({
					disabled: 'disabled',
					readonly: 'readonly'
				});
				$('.saveDiv').hide();
			} else {
				$('[name=finishTime],[name=period]').datepicker({
					dateFormat: "yy-mm-dd",
					dayNamesMin: ["一", "二", "三", "四", "五", "六", "日"],
					monthNamesShort: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
					changeMonth: true,
					changeYear: true,
					firstDay: 0,
					prevText: '上月',
					nextText: '下月'
				});

			}
			$('.comCheckDiv .saveDiv .savebutton').click(() => {
				this.save();
			});
			if(this.lishiid!==''){
				$('.saveDiv').hide();
			}
		}
		private out_trade_no: string = '';
		private lishiid: string = '';
		private html: JQuery;
		private showxw() {
			this.html = $(`<div class="msgbody">
                <div class="msgBox">
                    <div class="boxcon">跟单验货报告是公司填写还是工厂填写?确定后无法再次修改</div>
                    <div class="butarray clearfix">
                        <a class="button gongchang">工厂</a>
                        <a class="button gongsi ">公司</a>
                    </div>
                </div>
			</div>`);
			this.html.find('.gongchang').click(() => {
				this.upqx('fid');
			});
			this.html.find('.gongsi').click(() => {
				this.upqx('cid');
			});
			$('.comCheckDiv').append(this.html);
		}
		private upqx(check_type: string) {
			MC.api('comCheckUid', {
				out_trade_no: this.out_trade_no,
				check_type: check_type
			}, (json: any) => {
				this.html.remove();
				//	MC.msg.show("设置成功");
				setTimeout(function () {
					location.reload();
				}, 1500);
			});
		}

		/**
		 * 保存表单数据
		 */
		private save() {
			let list = $('#stylelist [name]');


			list = $('#thrumlist [name][type=radio]:checked');
			let thrumlist: any = {};
			for (let i = 0; i < list.length; i++) {
				thrumlist[$(list[i]).attr('name')] = $(list[i]).val();
			}

			list = $('#thrumlist [name][type=text]');

			for (let i = 0; i < list.length; i++) {
				thrumlist[$(list[i]).attr('name')] = $(list[i]).val();
			}


			let detail = $('[name=detail]').val();
            let wanchengdu = $('[name=wanchengdu]').val();
            let staffTotalNum = $('[name=staffTotalNum]').val();
			let staffStyleNum = $('[name=staffStyleNum]').val();
			let finishTime = $('[name=finishTime]').val();

			let fuzeren_name = $('[name=fuzeren_name]').val();
			let fuzeren_tel = $('[name=fuzeren_tel]').val();

			let obj = {
				stylelist: this.stylelist.getDataJson(),
				sizelist: this.sizelist.getDataJson(),
				thrumlist: JSON.stringify(thrumlist),


				detail: detail,
                staffTotalNum: staffTotalNum,
                wanchengdu: wanchengdu,
				staffStyleNum: staffStyleNum,
				finishTime: finishTime,

				fuzeren_name: fuzeren_name,
				fuzeren_tel: fuzeren_tel,
				out_trade_no: this.out_trade_no
			};
			MC.apiD('savecomCheck', obj, (json: any) => {
				MC.msg.show("保存成功");
			});
		}

	}
}